// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TransactionsTabViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="TransactionsTabViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Transaction.ViewModel
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Windows.Input;

    using Hisaab.Business.Interfaces;
    using Hisaab.Client.Common.BaseClasses;
    using Hisaab.Client.Model;
    using Hisaab.Common;

    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="TransactionsTabViewModel"/> type.
    /// </summary>
    public class TransactionsTabViewModel : ContextualTabViewModel
    {
        /// <summary>
        /// Member variable _accountManager
        /// </summary>
        private readonly IAccountManager _accountManager;

        /// <summary>
        /// Member variable _instance
        /// </summary>
        private static TransactionsTabViewModel instance;

        /// <summary>
        /// Member variable _accounts
        /// </summary>
        private ObservableCollection<Account> _accounts;

        /// <summary>
        /// Member variable _refreshCommand
        /// </summary>
        private ICommand _refreshCommand;

        /// <summary>
        /// Initializes a new instance of the <see cref="TransactionsTabViewModel"/> class.
        /// </summary>
        internal TransactionsTabViewModel()
            : this(ServiceLocator.Current.GetInstance<IAccountManager>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TransactionsTabViewModel"/> class.
        /// </summary>
        /// <param name="accountManager">The account manager.</param>
        internal TransactionsTabViewModel(IAccountManager accountManager)
        {
            Contract.Requires<ArgumentNullException>(accountManager != null);
            _accountManager = accountManager;
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static TransactionsTabViewModel Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new TransactionsTabViewModel();
                }

                return instance;
            }
        }

        /// <summary>
        /// Gets Refresh Command.
        /// </summary>
        public ICommand RefreshCommand
        {
            get
            {
                if (_refreshCommand == null)
                {
                    _refreshCommand = new DelegateCommand(OnRefresh);
                }

                return _refreshCommand;
            }
        }

        /// <summary>
        /// Gets or sets the accounts.
        /// </summary>
        /// <value>
        /// The accounts.
        /// </value>
        public ObservableCollection<Account> Accounts
        {
            get
            {
                if (_accounts == null)
                {
                    OnRefresh();
                }

                return _accounts;
            }

            set
            {
                RaisePropertyChangingEvent("Accounts");
                _accounts = value;
                RaisePropertyChangedEvent("Accounts");
            }
        }

        /// <summary>
        /// Called when [refresh].
        /// </summary>
        private void OnRefresh()
        {
            var accounts = _accountManager.List().Select(x => x.Map<Business.Entities.Account, Account>()).ToList();
            accounts.Add(new Account { AccountId = -1, AccountType = -1, Name = "All Accounts" });
            Accounts = new ObservableCollection<Account>(accounts);
        }
    }
}